# -*- coding:utf-8 -*-
from fastapi import APIRouter, Request, Depends, Query
from typing import Union
from core.curd import PageInfo, IDList, QueryWrapper
from core.response import tableinfo, success
from app.annotation import RequiresPermissions
from app.service.system import SysOrgService
from app.entity.system import CreateSysOrg, UpdateSysOrg
from core.utils import TreeUtil

router = APIRouter()


@router.get('/get')
@RequiresPermissions('sys:org:detail')
async def test(ids: int = Query(), mService=Depends(SysOrgService.instance)):
    """
        获取测试详情
    """
    one = await mService.select_by_id(ids)
    return success('OK', one)


@router.get('/list')
@RequiresPermissions('sys:org:list')
async def lists(mService=Depends(SysOrgService.instance)):
    """
        获取测试列表
    """
    rows = await mService.select_all()
    return success("OK", TreeUtil.build_tree_nodes(rows))


@router.post('/add')
@RequiresPermissions('sys:org:add')
async def add(post: CreateSysOrg, mService=Depends(SysOrgService.instance)):
    """
        添加测试
    """
    one = await mService.insert(post.dict())
    return success('OK', one)


@router.post('/edit')
@RequiresPermissions('sys:org:edit')
async def update(post: UpdateSysOrg, mService=Depends(SysOrgService.instance)):
    """
        编辑测试
    """
    one = await mService.update(post.dict())
    return success('OK', one)


@router.post('/del')
@RequiresPermissions('sys:org:remove')
async def remove(ids: IDList, mService=Depends(SysOrgService.instance)):
    """
        删除测试
    """
    one = await mService.delete_batch_ids(ids.ids)
    return success('OK')
